package com.xydtech.workflow.controller;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.workflow.domain.WflFlowdefine;
import com.xydtech.workflow.domain.WflFlowstep;
import com.xydtech.workflow.domain.WflFlowversion;
import com.xydtech.workflow.domain.WflSteplogic;
import com.xydtech.workflow.service.IWflFlowdefineService;
import com.xydtech.workflow.service.IWflFlowstepService;
import com.xydtech.workflow.service.IWflFlowversionService;
import com.xydtech.workflow.service.IWflSteplogicService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流程节点逻辑配置Controller
 *
 * @author JHON
 * @date 2021-06-07
 */
@Controller
@RequestMapping("/workflow/steplogic")
public class WflSteplogicController extends BaseController {
    private String prefix = "workflow/steplogic";

    @Autowired
    private IWflSteplogicService wflSteplogicService;
    @Autowired
    private IWflFlowstepService wflFlowstepService;
    @Autowired
    private IWflFlowdefineService wflFlowdefineService;
    @Autowired
    private IWflFlowversionService wflFlowversionService;

    /**
     * 查询流程节点逻辑配置列表
     */
    @RequiresPermissions("workflow:flowdefine:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WflSteplogic wflSteplogic) {
        startPage();
        List<WflSteplogic> list = wflSteplogicService.selectWflSteplogicList(wflSteplogic);
        return getDataTable(list);
    }


    /**
     * 新增流程节点逻辑配置
     */
    @GetMapping("/add/{versionId}/{orderNum}")
    public String add(@PathVariable("versionId") Long versionId, @PathVariable("orderNum") Long orderNum, ModelMap mmap) {
        //查询版本信息
        WflFlowversion wflFlowVersion = wflFlowversionService.selectWflFlowversionById(versionId);
        //查询流程定义信息
        WflFlowdefine flowDefine = wflFlowdefineService.selectWflFlowdefineById(wflFlowVersion.getFlowId());
        //查询流程节点信息
        WflFlowstep wflFlowstep = wflFlowstepService.selectWflFlowstepByVersionIdAndOrderNum(versionId, orderNum);
        mmap.put("versionId", versionId);
        mmap.put("stepId", wflFlowstep.getStepId());
        mmap.put("deptId", flowDefine.getDeptId());
        return prefix + "/add";
    }

    /**
     * 新增保存流程节点逻辑配置
     */
    @RequiresPermissions("workflow:flowdefine:add")
    @Log(title = "流程节点逻辑配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WflSteplogic wflSteplogic) {
        return toAjax(wflSteplogicService.insertWflSteplogic(wflSteplogic));
    }

    /**
     * 修改流程节点逻辑配置
     */
    @GetMapping("/edit/{logicId}")
    public String edit(@PathVariable("logicId") Long logicId, ModelMap mmap) {
        WflSteplogic wflSteplogic = wflSteplogicService.selectWflSteplogicById(logicId);
        mmap.put("wflSteplogic", wflSteplogic);
        return prefix + "/edit";
    }

    /**
     * 修改保存流程节点逻辑配置
     */
    @RequiresPermissions("workflow:flowdefine:edit")
    @Log(title = "流程节点逻辑配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WflSteplogic wflSteplogic) {
        return toAjax(wflSteplogicService.updateWflSteplogic(wflSteplogic));
    }

    /**
     * 删除流程节点逻辑配置
     */
    @RequiresPermissions("workflow:flowdefine:remove")
    @Log(title = "流程节点逻辑配置", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(wflSteplogicService.deleteWflSteplogicByIds(ids));
    }
}
